#include "myc.h"

void prtype(struct vtype vt) {
	printf("%s",vt.bt->name);
	int i;
	for (i=0;i<vt.pt;i++) printf("*");
	if (vt.lv) printf("&");
}

void prtoken(struct token *tok) {
	int i;
	for (i=0;i<tok->len;i++) printf("%c",tok->str[i]);
}

void prtokens(struct token *tok) {
	while (tok) {
		prtoken(tok);
		printf("\n");
		tok=tok->next;
	}
}

void printtree(struct tree *tree) {
	static int ind=0;
	int i;
	if (!tree) return;
	for (i=0;i<ind;i++) printf(" ");
	switch (tree->toket) {
	case -1:
		prtoken(tree->to.token);
		printf("\n");
		break;
	case -2:
		printf("<");
		prtoken(tree->to.token);
		printf(">\n");
		break;
	case 1:
		printf("%s (",tree->to.func->name);
		prtype(tree->type);
		printf(")\n");
		break;
	case 2:
		printf("%hd (int)\n",tree->to.val);
		break;
	case 3:
		printf("%s (",tree->to.var->name);
		prtype(tree->type);
		printf(")\n");
		break;
	case 4:
		printf("\'%c\' (char)\n",tree->to.vchar);
		break;
	case 5:
		prtoken(tree->to.token);
		printf(" (char*)\n");
		break;
	}
	ind++;
	printtree(tree->down);
	ind--;
	printtree(tree->next);
}

void error(char *err) {
	prtokoff(pass);
	printf("%s\n",err);
	exit(1);
}

void tokerror(struct token *sta,struct token *end,char *err) {
	prtokoff(sta);
	printf("%s ",err);
	while (sta!=end) {
		printf(" ");
		prtoken(sta);
		sta=sta->next;
	}
	printf("\n");
	exit(1);
}

void treerror(struct tree *tree,char *err) {
	prtokoff(pass);
	printf("%s\n",err);
	printtree(tree);
	exit(1);
}
